<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    public function login()
    {
        return view("admin.login");
    }

    public function logout()
    {
        session()->flush();
        return redirect()->route("adminIndex");

    }

    public function doLogin(Request $request)
    {

        $phone = $request->post("phone");
        $password = $request->post("password");

        $user = User::where("user_phone", $phone)->first();



        if (!$user){
            session()->flash("error", "用户不存在");
            return redirect()->route("adminLogin");
        }

        if (!in_array($user->role_id, [1,2])){
            session()->flash("error", "权限不足");
            return redirect()->route("adminLogin");
        }

        if (!password_verify($password, $user->user_password)){
            session()->flash("error", "密码错误");
            return redirect()->route("adminLogin");
        }
        session([
            "is_login" => true,
            "user_name" => "用户",
        ]);
        return redirect()->route("adminIndex");
    }
}
